package impl

import (
	"blog/internal/models"
	"gorm.io/gorm"
)

type UserRepositoryImpl struct {
	db *gorm.DB
}

func NewUserRepository(db *gorm.DB) *UserRepositoryImpl {
	return &UserRepositoryImpl{db: db}
}

// FindByUsername 根据用户名获取用户
func (u *UserRepositoryImpl) FindByUsername(username string) (*models.User, error) {
	var user models.User
	if err := u.db.Where("username = ?", username).First(&user).Error; err != nil {
		return nil, err
	}
	return &user, nil
}

// FindById 根据ID获取用户信息
func (u *UserRepositoryImpl) FindById(userId int64) (*models.User, error) {
	var user models.User
	if err := u.db.Where("id = ?", userId).First(&user).Error; err != nil {
		return nil, err
	}
	return &user, nil
}

func (u *UserRepositoryImpl) UpdateLastLogin(user *models.User) error {
	if err := u.db.Save(user).Error; err != nil {
		return err
	}
	return nil
}
